package com.taoye.thread;

import cn.hutool.core.util.RandomUtil;

/**
 * join方法遇到了interrupt方法
 *
 * @author : Mr.Tao
 * @version : 1.0
 * @date : 2022/4/4 10:31
 **/
public class Demo47 {
    public static void main(String[] args) throws InterruptedException {
        Demo47ThreadB t2 = new Demo47ThreadB();
        Thread t3 = new Demo47ThreadC(t2);
        t2.start();
        t3.start();
    }
}


class Demo47ThreadA extends Thread {

    @Override
    public void run() {
        //模拟耗时操作
        System.out.println("A开始耗时操作了");
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            String s = new String();
            String s1 = RandomUtil.randomString(9);
        }
        System.out.println("A结束耗时操作了");
    }

}

class Demo47ThreadB extends Thread {

    @Override
    public void run() {
        try {
            Demo47ThreadA t = new Demo47ThreadA();
            t.start();
            t.join();
            System.out.println("B正常结束了");
        } catch (InterruptedException e) {
            System.out.println("B异常结束了");
            e.printStackTrace();
        }
    }
}

class Demo47ThreadC extends Thread {
    private Demo47ThreadB t;

    public Demo47ThreadC(Demo47ThreadB t) {
        this.t = t;
    }

    @Override
    public void run() {
        t.interrupt();
    }
}